﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TRAC_Demo
{
    // T.R.A.C.
    // Main Author: Morgan Davis
    // Additional Authors: Ryan Redburn
    // Date: 9/30/2013
    // Updated: 10/20/2013 removed list box and replaced with datagridview, rearranged button layout and added button to 
    //                     access the internal messenging feature (mail)

    public partial class FormGeneralOverview : Form
    {
        #region [ Fields ]

        TRACobjectsDataContext dataObject = new TRACobjectsDataContext();

        #endregion

        #region [ Constructors ]

        public FormGeneralOverview()
        {
            InitializeComponent();
            this.Activated += new EventHandler(FormGeneralOverview_Activated);
            this.BackColor = CurrentUser.UserColor;
        }

        #endregion

        #region [ Events ]

        // Updates the ticket data grid on load
        private void FormGeneralOverview_Load(object sender, EventArgs e)
        {
            UpdateDataGrid();
        }

        // Opens up the report view
        private void btnReport_Click(object sender, EventArgs e)
        {
            FormGenerateReport GenerateReport = new FormGenerateReport();
            GenerateReport.Show();
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            // Open a new generate ticket form
            FormGenerateTicket FormGenerate = new FormGenerateTicket();
            FormGenerate.ShowDialog();
        }

        // Opens the account management form appropriate for the current user's privileges
        private void btnManagement_Click(object sender, EventArgs e)
        {
            if (CurrentUser.Privileges == "Administrator")
            {
                FormAdminManagement AdminManagement = new FormAdminManagement();
                AdminManagement.ShowDialog();
                this.BackColor = CurrentUser.UserColor;
            }
            else if (CurrentUser.Privileges == "Standard")
            {
                FormStandardManagement StandardManagement = new FormStandardManagement();
                StandardManagement.ShowDialog();
                this.BackColor = CurrentUser.UserColor;
            }
        }

        // Launches mail for the current user
        private void btnMail_Click(object sender, EventArgs e)
        {
            FormMail FormMail = new FormMail();
            FormMail.ShowDialog();
        }

        private void btnLogOut_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to log out?", "Confirm Log Out",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                this.Close();
            }
        }

        // Exits the app on user confirmation
        private void btnExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to exit?", "Confirm Exit",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Application.Exit();
            }
        }

        // Event handler that autorefreshes the ticket lists when focus is returned to the main form
        private void FormGeneralOverview_Activated(object sender, EventArgs e)
        {
            UpdateDataGrid();
        }

        #endregion

        #region [ Methods ]

        // updates the ticket list boxes
        private void UpdateDataGrid()
        {
            //Clear list and combo boxes before updating and re-populating
            dataGridReport.Columns.Clear();
            DataGridViewButtonColumn col = new DataGridViewButtonColumn();
            col.UseColumnTextForButtonValue = true;
            col.Name = "View Ticket";
            col.Text = "View Ticket";
            dataGridReport.Columns.Add(col);

            //pull all tickets from ticket table and place them in the recent ticket list box
            var myQuery = (from t in dataObject.Tickets
                           join e in dataObject.Employees on t.EmployeeID
                           equals e.EmployeeID
                           select new { t.TicketNumber, t.DateCreated, e.lName, e.fName, t.StatusCode }).Take(25);
            dataGridReport.DataSource = myQuery;
            dataGridReport.Columns[1].Width = 80;
            dataGridReport.Columns[2].Width = 150;
        }
        //Method that handles the click event when button in datagrid is pressed
        private void dataGridReport_CellClick(object sender,
                    System.Windows.Forms.DataGridViewCellEventArgs e)
        {
            if (dataGridReport.Columns[e.ColumnIndex].Name == "View Ticket")
            {
                int col = this.dataGridReport.CurrentCell.ColumnIndex;
                int row = this.dataGridReport.CurrentCell.RowIndex;
                int valueToPass = (int)(dataGridReport.Rows[row].Cells[1].Value);
                if (valueToPass != 0)//make sure value of 0 is not passed to the database
                {
                    FormTicketOverview FormTicketOverview = new FormTicketOverview(valueToPass);
                    FormTicketOverview.Show();
                }
                else
                    MessageBox.Show("Ticket Number Doesn't exist in the DataBase", "Ticket Doesn't Exist!");
            }
        }

        #endregion

        
    }
}
